package com.bw.module_video_detail.mvi.viewmodel

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.bw.lib_common.base.BaseViewModel
import com.bw.module_video_detail.mvi.intent.VideoDetailIntent
import com.bw.module_video_detail.mvi.model.repository.VideoDetailRepository
import com.bw.module_video_detail.mvi.state.BulletScreenState
import com.bw.module_video_detail.mvi.state.ModifyCoinAmountState
import com.bw.module_video_detail.mvi.state.PublishBulletScreenState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author ytx
 * @date 2025-10-21 09:19
 * @description
 */
@HiltViewModel
class VideoDetailViewModel @Inject constructor(private val repository: VideoDetailRepository):BaseViewModel<VideoDetailIntent>() {

    private var bulletScreenState  = MutableStateFlow<BulletScreenState>(BulletScreenState.Init)
    val bulletScreenState_ = bulletScreenState.asStateFlow()


    private var publishBulletScreenState  = MutableStateFlow<PublishBulletScreenState>(PublishBulletScreenState.Init)
    val publishBulletScreenState_ = publishBulletScreenState.asStateFlow()

    private var modifyCoinAmountState  = MutableStateFlow<ModifyCoinAmountState>(ModifyCoinAmountState.Init)
    val modifyCoinAmountState_ = modifyCoinAmountState.asStateFlow()
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is VideoDetailIntent.getBulletScreenInfo -> {
                        //获得弹幕信息
                        getBulletScreenInfo(it)
                    }
                    is VideoDetailIntent.publishBulletScreen -> {
                        //发送弹幕
                        publishBulletScreen(it)
                    }
                    is VideoDetailIntent.modifyCoinAmount -> {
                        //修改金币
                        modifyCoinAmount(it)
                    }
                }
            }
        }

    }

    private fun modifyCoinAmount(it: VideoDetailIntent.modifyCoinAmount) {
        viewModelScope.launch {
           repository.modifyCoinAmount(it.accountId,it.nm)
               .catch {
                   modifyCoinAmountState.value = ModifyCoinAmountState.Error
               }.filter {
                   it != null
               }.collect{
                   if (it.code == 200){
                       if (it.data){
                           modifyCoinAmountState.value = ModifyCoinAmountState.Success
                       }else{
                           modifyCoinAmountState.value = ModifyCoinAmountState.Error
                       }
                   }else{
                       modifyCoinAmountState.value = ModifyCoinAmountState.Error
                   }
               }
        }
    }

    private fun publishBulletScreen(intent: VideoDetailIntent.publishBulletScreen){
        viewModelScope.launch {
            repository.publishBulletScreen(intent.entity)
                .catch { it->
                    Log.d("TAG", "publishBulletScreen: "+it.message)
                    publishBulletScreenState.value = PublishBulletScreenState.Error
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        publishBulletScreenState.value = PublishBulletScreenState.Success(it.data)
                    }else{
                        publishBulletScreenState.value = PublishBulletScreenState.Error
                    }
                }
        }
    }

    /**
     * 获得弹幕信息
     */
    private fun getBulletScreenInfo(intent: VideoDetailIntent.getBulletScreenInfo){
        viewModelScope.launch {
            repository.getBulletScreenInfo(intent.datatype, intent.itemId)
                .catch {
                    bulletScreenState.value = BulletScreenState.Error
                }.filter {
                    it != null
                }.collect{
                    if (it.code == 200){
                        if (it.data.isNullOrEmpty()){
                            bulletScreenState.value = BulletScreenState.Empty
                        }else{
                            bulletScreenState.value = BulletScreenState.Success(it.data)
                        }
                    }else{
                        bulletScreenState.value = BulletScreenState.Error
                    }
                }
        }
    }
}